package com.qf.lanzhou.blog.api.controller;

import com.qf.lanzhou.blog.api.pojo.entity.User;
import com.qf.lanzhou.blog.api.service.UserService;
import com.qf.lanzhou.blog.api.service.impl.UserServiceImp;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;

/**
 * @author Yeach-chen
 * @ClassName ModifyServlet
 */
@WebServlet("/modify.do")
public class ModifyServlet extends BaseServletPost{
    private UserService userService=new UserServiceImp();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取新密码
        String newPassword = req.getParameter("newpassWord");
        System.out.println("新密码："+newPassword);
        //获取用户名
        User user = (User) req.getSession().getAttribute("user");
        int result;
        try{
            result = userService.modifyUser(newPassword, user);
            System.out.println("旧密码："+user.getPassword());
            successResultWrite(result,resp);
            req.removeAttribute("user");
        }catch (RuntimeException e){
            String message = e.getMessage();
            failedResultWrite(message,resp);
        }

    }
}
